package com.itheima.bakery.demos.web.service.impl;

import com.itheima.bakery.demos.web.Repositories.ProductRepository;
import com.itheima.bakery.demos.web.model.Order;
import com.itheima.bakery.demos.web.model.OrderDetail;
import com.itheima.bakery.demos.web.Repositories.OrderDetailRepository;
import com.itheima.bakery.demos.web.model.Product;
import com.itheima.bakery.demos.web.service.OrderDetailService;
import com.itheima.bakery.demos.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductRepository productRepository;

    @Override
    public List<OrderDetail> findAllOrderDetails() {
        return orderDetailRepository.findAll();
    }

    @Override
    public Optional<OrderDetail> findOrderDetailById(Integer id) {
        return orderDetailRepository.findById(id);
    }

    @Override
    public OrderDetail saveOrderDetail(OrderDetail orderDetail) {
        OrderDetail savedDetail = orderDetailRepository.save(orderDetail);
        if (savedDetail.getOrderDetailID() != null) {
            // 只有在保存成功后才更新订单总金额
            orderService.updateOrderTotal(savedDetail.getOrder().getOrderID());
        }
        return savedDetail;
    }

    @Override
    public void deleteOrderDetail(Integer id) {
        OrderDetail orderDetail = orderDetailRepository.findById(id).orElse(null);
        if (orderDetail != null) {
            orderDetailRepository.deleteById(id);
            orderService.updateOrderTotal(orderDetail.getOrder().getOrderID());
        }
    }
    @Override
    public OrderDetail createOrderDetail(Integer orderId, Integer productId, Integer quantity) {
        // 确保Order对象已经存在，并且有了一个有效的ID
        Order order = orderService.findOrderById(orderId)
                .orElseThrow(() -> new RuntimeException("Order not found with ID: " + orderId));

        // 从Products表中读取Price值
        Product product = productRepository.findById(productId)
                .orElseThrow(() -> new RuntimeException("Product not found with ID: " + productId));
        BigDecimal unitPrice = product.getPrice();

        // 创建OrderDetail对象
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrder(order); // 关联Order对象
        orderDetail.setProduct(product);
        orderDetail.setQuantity(quantity);
        orderDetail.setUnitPrice(unitPrice);
        orderDetail.setSubtotal(unitPrice.multiply(new BigDecimal(quantity))); // 计算小计

        // 保存OrderDetail对象
        return orderDetailRepository.save(orderDetail);
    }
    @Override
    public OrderDetail updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailRepository.save(orderDetail);
    }
}